package com.istock.union.user.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.istock.base.cache.DeleteCache;
import com.istock.base.cache.UseCache;
import com.istock.base.common.api.model.PageInfo;
import com.istock.base.mybatis.utils.MybatisFieldConvertUtils;
import com.istock.union.user.dao.ConfigInfoMapper;
import com.istock.union.user.model.ConfigInfo;
import com.istock.union.user.model.ConfigInfoExample;
import com.istock.union.user.model.OrderByModel;
import com.istock.union.user.service.ConfigInfoService;

@Repository("ConfigInfoService")
@Primary
public class ConifgInfoServiceImpl implements ConfigInfoService{

	@Autowired
	private ConfigInfoMapper configInfoMapper;
	
	public List<ConfigInfo> findConfigInfo(ConfigInfo configInfo , PageInfo<ConfigInfo> page,OrderByModel orderBy){
		ConfigInfoExample example = new ConfigInfoExample();
		ConfigInfoExample.Criteria criteria = example.createCriteria();
		
		if(configInfo != null) {
			if(StringUtils.isNotBlank(configInfo.getConfigKey())) {
				criteria.andConfigKeyLike("%" + configInfo.getConfigKey() + "%");
			}
			
			if(StringUtils.isNotBlank(configInfo.getConfigName())) {
				criteria.andConfigNameLike("%" + configInfo.getConfigName() + "%");
			}
		}
		if(orderBy == null) {
			example.setOrderByClause("CREATE_TIME DESC");
		}else {
			example.setOrderByClause(MybatisFieldConvertUtils.getInstance().unmarshal("com.istock.union.user.dao.ConfigInfoMapper", "BaseResultMap", orderBy.getOrderByColumn())+" "+orderBy.getIsAsc());
		}
		
		
		return configInfoMapper.selectByExample(example, page);
	}
	
	public ConfigInfo findById(String id) {
		return configInfoMapper.selectByPrimaryKey(id);
	}
	
	@UseCache(key="'CONFIG_'+#key")
	public ConfigInfo findByKey(String key){
		ConfigInfoExample example = new ConfigInfoExample();
		ConfigInfoExample.Criteria criteria = example.createCriteria();
		criteria.andConfigKeyEqualTo(key);
		List<ConfigInfo> configList = configInfoMapper.selectByExample(example);
		
		if(configList != null){
			return configList.get(0);
		}
		return null;
	}
	
	@DeleteCache(key="'CONFIG_'+#configInfo.configKey")
	@Transactional
	public void saveConfigInfo(ConfigInfo configInfo) {
		if(configInfo != null) {
			if(StringUtils.isNotBlank(configInfo.getId())) {
				configInfoMapper.updateByPrimaryKeySelective(configInfo);
			}else {
				configInfoMapper.insert(configInfo);
			}
		}
	}
	
	public boolean checkConfigKeyUnique(String id , String configKey){
		ConfigInfoExample example = new ConfigInfoExample();
		ConfigInfoExample.Criteria criteria = example.createCriteria();
		
		criteria.andIdNotEqualTo(id);
		criteria.andConfigKeyEqualTo(configKey);
		
		return configInfoMapper.countByExample(example) == 0;
	}
	
	public void deleteConfigInfo(String id) {
		configInfoMapper.deleteByPrimaryKey(id);
	}
}
